package liangtWorkSpace.threadDemo.thread.JUC;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author liangt
 * @ClassName: ReentrantLockSimple
 * @title: ReentrantLockSimple
 * @projectName liangtWorkSpace.threadDemo.thread.JUC
 * @description: 重入锁一定要手动调用解锁，不然会发生死锁
 * @date 2020/11/5 19:05
 */
public class ReentrantLockSimple {
    private static ReentrantLock lock = new ReentrantLock();
    private static int count = 0;
    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newFixedThreadPool(10);


        for (int i = 0; i <= 100; i++) {
            int index = i;
            threadPool.execute(new Runnable() {
                @Override
                public void run() {
                    go(index);
                }
            });
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(count);
        threadPool.shutdownNow();
    }

    private static void go(int idenx){
        lock.lock();
        count = count+idenx;
        lock.unlock();
    }
}
